<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>订单管理</title>
    <style>
        body {
            font-family: "Arial", sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(to right, #aed581, #81c784);
            color: #333;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
        }

        .container {
            width: 90%;
            max-width: 1200px;
            margin: 20px 0;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
        }

        .header h1 {
            margin: 0;
            color: #388e3c;
        }

        .toolbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .search-bar {
            display: flex;
            gap: 10px;
            flex-grow: 1;
        }

        .search-bar input {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .search-bar button {
            background: #388e3c;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .search-bar button:hover {
            background: #66bb6a;
        }

        .order-list {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }

        .order-card {
            background: #f9f9f9;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            padding: 20px;
            text-align: left;
        }

        .order-card h3 {
            margin: 0 0 10px;
            color: #388e3c;
        }

        .order-card p {
            margin: 5px 0;
            color: #555;
        }

        .order-card .actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 15px;
        }

        .order-card .actions a {
            text-decoration: none;
            padding: 5px 10px;
            border-radius: 5px;
            background: #e8f5e9;
            color: #388e3c;
            transition: background 0.3s;
        }

        .order-card .actions a:hover {
            background: #c8e6c9;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 10px;
        }

        .pagination a {
            padding: 5px 15px;
            text-decoration: none;
            border-radius: 5px;
            background: #f1f1f1;
            color: #333;
            transition: background 0.3s;
        }

        .pagination a.active {
            font-weight: bold;
            background: #388e3c;
            color: white;
        }

        .pagination a:hover {
            background: #e0e0e0;
        }

        #qrModal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
        }

        #qrModal .modal-content {
            background: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            width: 300px;
        }

        #qrModal img {
            width: 200px;
            height: auto;
            margin-bottom: 20px;
        }

        #qrModal button {
            margin: 10px;
            padding: 10px 20px;
            background: #388e3c;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        #qrModal button:hover {
            background: #66bb6a;
        }
        .add-category {
            background: #81c784;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background 0.3s;
        }

        .add-category:hover {
            background: #66bb6a;
        }

    </style>
</head>
<body>
<div class="container">
    <div class="header">
        <h1>订单管理</h1>
    </div>
    <div class="toolbar">
        <form method="get" action="OrderServlet" class="search-bar">
            <input type="text" name="search" placeholder="搜索订单" value="${param.search}" />
            <button type="submit">搜索</button>
        </form>
        <a href="admin.jsp" class="add-category">返回</a>
    </div>
    <div class="order-list">
        <c:forEach var="order" items="${orders}">
            <c:if test="${sessionScope.user.role==1}">
                <div class="order-card">
                    <h3>订单 ID: ${order.id}</h3>
                    <p><strong>用户:</strong> ${order.username}</p>
                    <p><strong>菜品:</strong> ${order.menuname}</p>
                    <p><strong>数量:</strong> ${order.count}</p>
                    <p><strong>总价:</strong> ￥${order.amount}</p>
                    <p><strong>状态:</strong>
                        <c:choose>
                            <c:when test="${order.status == 1}">待付款</c:when>
                            <c:when test="${order.status == 2}">已付款</c:when>
                            <c:when test="${order.status == 3}">已下单</c:when>
                        </c:choose>
                    </p>
                    <p><strong>创建时间:</strong> ${order.cretime}</p>
                    <div class="actions">
                        <a href="editorders.jsp?id=${order.id}">编辑</a>
                        <a href="OrderServlet?action=delete&id=${order.id}"
                           onclick="return confirm('确认删除吗？');">删除</a>
                        <c:if test="${sessionScope.user.role==3 && order.status==1}">
                            <a href="javascript:void(0);" onclick="showQRCode(${order.id})">提交</a>
                        </c:if>
                    </div>
                </div>
            </c:if>

            <c:if test="${sessionScope.user.role==2 && order.status==3}">
                <div class="order-card">
                    <h3>订单 ID: ${order.id}</h3>
                    <p><strong>用户:</strong> ${order.username}</p>
                    <p><strong>菜品:</strong> ${order.menuname}</p>
                    <p><strong>数量:</strong> ${order.count}</p>
                    <p><strong>总价:</strong> ￥${order.amount}</p>
                    <p><strong>状态:</strong>
                        <c:choose>
                            <c:when test="${order.status == 1}">待付款</c:when>
                            <c:when test="${order.status == 2}">已付款</c:when>
                            <c:when test="${order.status == 3}">已下单</c:when>
                        </c:choose>
                    </p>
                    <p><strong>创建时间:</strong> ${order.cretime}</p>
                    <div class="actions">

                        <c:if test="${sessionScope.user.role==3 && order.status==1}">
                            <a href="javascript:void(0);" onclick="showQRCode(${order.id})">提交</a>
                        </c:if>
                    </div>
                </div>

            </c:if>
            <c:if test="${order.userid == sessionScope.user.userId && sessionScope.user.role==3}">
                <div class="order-card">
                    <h3>订单 ID: ${order.id}</h3>
                    <p><strong>用户:</strong> ${order.username}</p>
                    <p><strong>菜品:</strong> ${order.menuname}</p>
                    <p><strong>数量:</strong> ${order.count}</p>
                    <p><strong>总价:</strong> ￥${order.amount}</p>
                    <p><strong>状态:</strong>
                        <c:choose>
                            <c:when test="${order.status == 1}">待付款</c:when>
                            <c:when test="${order.status == 2}">已付款</c:when>
                            <c:when test="${order.status == 3}">已下单</c:when>
                        </c:choose>
                    </p>
                    <p><strong>创建时间:</strong> ${order.cretime}</p>
                    <div class="actions">

                        <c:if test="${sessionScope.user.role==3 && order.status==1}">
                            <a href="javascript:void(0);" onclick="showQRCode(${order.id})">提交</a>
                        </c:if>
                    </div>
                </div>

            </c:if>




        </c:forEach>
    </div>
    <div class="pagination">
        <c:forEach var="page" begin="1" end="${totalPages}">
            <a href="OrderServlet?page=${page}&search=${param.search}"
               class="${page == currentPage ? 'active' : ''}">${page}</a>
        </c:forEach>
    </div>
</div>

<!-- 支付二维码模态框 -->
<div id="qrModal">
    <div class="modal-content">
        <h3>支付二维码</h3>
        <img src="https://img0.baidu.com/it/u=582752653,3386538207&fm=253&fmt=auto&app=138&f=GIF?w=500&h=500" alt="QR Code" />
        <button onclick="confirmPayment()">确认</button>
        <button onclick="closeQRCode()">取消</button>
    </div>
</div>

<script>
    function showQRCode(orderId) {
        sessionStorage.setItem('currentOrderId', orderId);
        document.getElementById('qrModal').style.display = 'flex';
    }

    function confirmPayment() {
        // 获取当前订单 ID
        const orderId = sessionStorage.getItem('currentOrderId');
        if (orderId) {
            // 拼接请求 URL
            const requestUrl = 'OrderServlet?action=confirmPayment&id=' + orderId;

            // 发送请求更新订单状态
            fetch(requestUrl, {
                method: 'POST'
            })
                .then(response => {
                    if (response.ok) {
                        alert('支付成功！');
                        location.reload(); // 刷新页面以显示更新后的状态
                    } else {
                        alert('支付失败，请重试！');
                    }
                });
        }
        closeQRCode();
    }


    function closeQRCode() {
        document.getElementById('qrModal').style.display = 'none';
    }
</script>
</body>
</html>
